import 'package:flutter/material.dart';
import 'app_localizations.dart';
import 'language_view_model.dart';
import 'home_page_vm.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  late HomePageViewModel homeVm;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback(
          (_) => print('addPostFrameCallback'),
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final localizations = AppLocalizations.of(context)!;
    homeVm = HomePageViewModel(localizations.translate);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(homeVm.appTitle), // 从ViewModel获取文本
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // 欢迎消息
            Text(
              homeVm.welcomeMessage,
              style: Theme.of(context).textTheme.headlineMedium,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 20),

            // 示例内容
            Text(
              homeVm.greeting,
              style: Theme.of(context).textTheme.titleLarge,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 20),

            Text(
              homeVm.description,
              style: Theme.of(context).textTheme.bodyLarge,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 40),

            // 语言选择
            Text(
              homeVm.selectLanguage,
              style: Theme.of(context).textTheme.titleMedium,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 16),

            // 语言选择按钮
            Wrap(
              alignment: WrapAlignment.center,
              spacing: 10,
              runSpacing: 10,
              children: [
                ElevatedButton(
                  onPressed: () => context.languageViewModel.setEnglish(),
                  child: Text(homeVm.english),
                ),
                ElevatedButton(
                  onPressed: () => context.languageViewModel.setChinese(),
                  child: Text(homeVm.chinese),
                ),
                homeVm.isABC
                    ? ElevatedButton(
                  onPressed: () => context.languageViewModel.setJapanese(),
                  child: Text(homeVm.japanese),
                )
                    : Container(),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
